package com.boco.webmaster.model;

import com.boco.transnms.client.model.base.BoCmdFactory;
import java.util.Map;
import java.util.HashMap;
import com.boco.transnms.common.dto.base.DataObjectList;
import java.util.Iterator;
import com.boco.transnms.common.dto.base.BoActionContext;
import com.boco.transnms.common.dto.Floor;
import com.boco.transnms.client.model.base.IBoCommand;
import com.boco.transnms.server.bo.helper.cm.FloorBOHelper;

public class FloorModel {
    private static FloorModel instance = new FloorModel();
    private Map floorMap = new HashMap();
    public FloorModel() {
        refresh();
    }

    public void refresh(){
        IBoCommand cmd = BoCmdFactory.getInstance().createBoCmd(FloorBOHelper.ActionName.getAllFloor, new Object[] {new BoActionContext()});
        DataObjectList dbos = (DataObjectList) cmd.exec();
        floorMap.clear();
        for (int i = 0; i < dbos.size(); i++) {
            Floor dbo = (Floor)dbos.get(i);
            floorMap.put(dbo.getCuid(),dbo);
        }
    }

    public static FloorModel getInstance() {
        return instance;
    }

    public Floor getFloorByCuid(String cuid){
        return (Floor)floorMap.get(cuid);
    }

    public DataObjectList getFloorBySiteCuid(String siteCuid){
        DataObjectList dbos = new DataObjectList();
        Iterator i = floorMap.keySet().iterator();
        while (i.hasNext()) {
            String floor_cuid = (String) i.next();
            Floor dbo = (Floor) floorMap.get(floor_cuid);
            if(dbo.getRelatedSpaceCuid().equals(siteCuid)){
                dbos.add(dbo);
            }
        }
        return dbos;
    }

    public String getCuidByNumber(long floorNumber,String siteCuid){
        Iterator i = floorMap.keySet().iterator();
        while (i.hasNext()) {
            String Floor_cuid = (String) i.next();
            Floor dbo = (Floor) floorMap.get(Floor_cuid);
            if(dbo.getFloorNum()==floorNumber && dbo.getRelatedSpaceCuid().equals(siteCuid)){
                return dbo.getCuid();
            }
        }
        return null;
    }

    public void addFloor(Floor dbo){
        floorMap.put(dbo.getCuid(),dbo);
    }

    public void delFloor(Floor dbo){
        floorMap.remove(dbo.getCuid());
    }

    public void modifyFloor(Floor dbo){
        floorMap.remove(dbo.getCuid());
        floorMap.put(dbo.getCuid(),dbo);
    }
}
